/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.service.client;

import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.io.HessianRemoteObject;
import com.jspx.txweb.service.WebBeanFactory;
import java.lang.reflect.Proxy;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 11-6-27
 * Time: 下午10:40
 *  jspx.net 提供的Hessian 调用接口
 *  当然你也可以使用原生接口调用方式
 *
 */
public class ClientHessianProxyFactory extends HessianProxyFactory {
    private String sessionId;
    public ClientHessianProxyFactory() {

    }

    /**
     * 实现开放接口应用登陆得到sessinId
     * @param url  url
     * @param appId  ConnectApp中的ID
     * @param timeMillis  时间验证
     * @param verify   验证码
     * @return 登录信息 json
     * @throws Exception  异常
     */
    public String login(String url,long appId, String loginId,String password,int passwordEncoded,long timeMillis,String verify) throws Exception
    {
        WebBeanFactory hessianAPI = (WebBeanFactory) create(WebBeanFactory.class,url,ClassLoader.getSystemClassLoader());
        return hessianAPI.login(appId, loginId,password,passwordEncoded,timeMillis, verify);
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    @Override
    public Object create(Class api, String urlName) throws MalformedURLException {
        return create(api, urlName, ClassLoader.getSystemClassLoader());
    }

    @Override
    public Object create(Class api, String urlName, ClassLoader loader) throws MalformedURLException {
        if (api == null) throw new NullPointerException("api must not be null for HessianProxyFactory.create()");
        return Proxy.newProxyInstance(loader, new Class[]{api, HessianRemoteObject.class}, new JspxHessianProxy(new URL(urlName), this,sessionId));
    }

}